using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class SnapScroller : MonoBehaviour
{
	private int panCount;

	[Range(1f, 500f)]
	public int panOffset;

	[Range(1f, 20f)]
	public float snapSpeed;

	[Range(1f, 10f)]
	public float scaleOffset;

	[Range(1f, 20f)]
	public float scaleSpeed;

	[Header("scrollRect")]
	public ScrollRect scrollRect;

	public GameObject GO_NeedGemAmount;

	public TextMeshProUGUI TXT_NeedGemAmount;

	private GameObject[] instPans;

	private Vector2[] pansPos;

	private Vector2[] pansScale;

	private RectTransform contentRect;

	private Vector2 contentVector;

	private int selectedPanID;

	private int currentSelectedPanID;

	private bool isScrolling;

	private Image img;

	private RectTransform rect;

	private Coroutine co;

	private WeaponAbility SCRT_WeaponAbility;

	private int needHighscoreForUnlockingCloak;

	private void Start()
	{
		InitWeapons();
	}

	public void InitWeapons()
	{
		if (CanvasList.instance.ShowingKindOfEquipList == "weaponList")
		{
			return;
		}
		CanvasList.instance.ShowingKindOfEquipList = "weaponList";
		currentSelectedPanID = -1;
		DestroyAllItemsInContentsScroller();
		contentRect = GetComponent<RectTransform>();
		panCount = Generator.instance.pool.WeaponPrefabList.Length;
		instPans = new GameObject[panCount];
		pansPos = new Vector2[panCount];
		pansScale = new Vector2[panCount];
		for (int i = 0; i < panCount; i++)
		{
			instPans[i] = Object.Instantiate(Generator.instance.pool.euipImage, base.transform.transform, false);
			rect = instPans[i].GetComponent<RectTransform>();
			rect.sizeDelta = new Vector2(200f, 200f);
			instPans[i].GetComponent<Image>().sprite = Generator.instance.pool.WeaponPrefabList[i].GetComponent<SpriteRenderer>().sprite;
			if (i != 0)
			{
				instPans[i].transform.localPosition = new Vector2(instPans[i].transform.localPosition.x, instPans[i - 1].transform.localPosition.y + rect.sizeDelta.y + (float)panOffset);
				pansPos[i] = -instPans[i].transform.localPosition;
			}
		}
		base.transform.localPosition = new Vector2(base.transform.position.x, pansPos[PlayerPrefs.GetInt("selectedWeaponID")].y);
	}

	public void InitHelm()
	{
		if (CanvasList.instance.ShowingKindOfEquipList == "helmList")
		{
			return;
		}
		CanvasList.instance.ShowingKindOfEquipList = "helmList";
		currentSelectedPanID = -1;
		DestroyAllItemsInContentsScroller();
		contentRect = GetComponent<RectTransform>();
		panCount = Sprites.instance.helmSpriteList.Length;
		instPans = new GameObject[panCount];
		pansPos = new Vector2[panCount];
		pansScale = new Vector2[panCount];
		for (int i = 0; i < panCount; i++)
		{
			instPans[i] = Object.Instantiate(Generator.instance.pool.euipImage, base.transform.transform, false);
			rect = instPans[i].GetComponent<RectTransform>();
			rect.sizeDelta = new Vector2(150f, 150f);
			instPans[i].transform.localEulerAngles = Vector2.zero;
			img = instPans[i].GetComponent<Image>();
			img.sprite = Sprites.instance.helmSpriteList[i];
			if (i != 0)
			{
				instPans[i].transform.localPosition = new Vector2(instPans[i].transform.localPosition.x, instPans[i - 1].transform.localPosition.y + rect.sizeDelta.y + (float)panOffset + 100f);
				pansPos[i] = -instPans[i].transform.localPosition;
			}
		}
		base.transform.localPosition = new Vector2(base.transform.position.x, pansPos[PlayerPrefs.GetInt("selectedHelmID")].y);
	}

	public void InitCloak()
	{
		if (CanvasList.instance.ShowingKindOfEquipList == "cloakList")
		{
			return;
		}
		CanvasList.instance.ShowingKindOfEquipList = "cloakList";
		currentSelectedPanID = -1;
		DestroyAllItemsInContentsScroller();
		contentRect = GetComponent<RectTransform>();
		panCount = VariousInfo.cloakInfo.Length;
		instPans = new GameObject[panCount];
		pansPos = new Vector2[panCount];
		pansScale = new Vector2[panCount];
		for (int i = 0; i < panCount; i++)
		{
			instPans[i] = Object.Instantiate(Generator.instance.pool.euipImage, base.transform.transform, false);
			rect = instPans[i].GetComponent<RectTransform>();
			rect.sizeDelta = new Vector2(150f, 150f);
			instPans[i].transform.localEulerAngles = Vector2.zero;
			img = instPans[i].GetComponent<Image>();
			img.sprite = Sprites.instance.etcSpriteList[6];
			img.color = VariousInfo.cloakInfo[i].c;
			if (i != 0)
			{
				instPans[i].transform.localPosition = new Vector2(instPans[i].transform.localPosition.x, instPans[i - 1].transform.localPosition.y + rect.sizeDelta.y + (float)panOffset + 100f);
				pansPos[i] = -instPans[i].transform.localPosition;
			}
		}
		base.transform.localPosition = new Vector2(base.transform.position.x, pansPos[PlayerPrefs.GetInt("selectedCloakID")].y);
	}

	private void DestroyAllItemsInContentsScroller()
	{
		int childCount = base.transform.childCount;
		if (childCount != 0)
		{
			for (int i = 0; i < childCount; i++)
			{
				Object.Destroy(base.transform.GetChild(i).gameObject);
			}
		}
	}

	private void OnEnable()
	{
		currentSelectedPanID = -1;
		if (co != null)
		{
			StopCoroutine(co);
			co = null;
		}
		co = StartCoroutine(IE_Update());
	}

	private IEnumerator IE_Update()
	{
		while (true)
		{
			yield return new WaitForSecondsRealtime(0.02f);
			if ((contentRect.anchoredPosition.y >= pansPos[0].y && !isScrolling) || (contentRect.anchoredPosition.y <= pansPos[pansPos.Length - 1].y && !isScrolling))
			{
				scrollRect.inertia = false;
			}
			for (int i = 0; i < panCount; i++)
			{
				float num = Mathf.Abs(contentRect.anchoredPosition.y - pansPos[i].y);
				if (num < 50f)
				{
					selectedPanID = i;
					ShowEquipStat();
				}
				float num2 = Mathf.Clamp(1f / (num / (float)panOffset) * scaleOffset, 0.5f, 1.2f);
				pansScale[i].x = Mathf.SmoothStep(instPans[i].transform.localScale.x, num2 + 0.3f, scaleSpeed * Time.fixedUnscaledDeltaTime);
				pansScale[i].y = Mathf.SmoothStep(instPans[i].transform.localScale.y, num2 + 0.3f, scaleSpeed * Time.fixedUnscaledDeltaTime);
				instPans[i].transform.localScale = pansScale[i];
			}
			float scrollVelocity = Mathf.Abs(scrollRect.velocity.y);
			if (scrollVelocity < 400f && !isScrolling)
			{
				scrollRect.inertia = false;
			}
			if (!isScrolling && !(scrollVelocity > 400f))
			{
				contentVector.y = Mathf.SmoothStep(contentRect.anchoredPosition.y, pansPos[selectedPanID].y, snapSpeed * Time.fixedUnscaledDeltaTime);
				contentRect.anchoredPosition = contentVector;
			}
		}
	}

	private void ShowEquipStat()
	{
		switch (CanvasList.instance.ShowingKindOfEquipList)
		{
			case "weaponList":
				ShowWeaponStat();
				break;
			case "helmList":
				ShowHelmStat();
				break;
			case "cloakList":
				ShowCloakStat();
				break;
		}
	}

	private void ShowWeaponStat()
	{
		if (currentSelectedPanID != selectedPanID)
		{
			currentSelectedPanID = selectedPanID;
			SCRT_WeaponAbility = Generator.instance.pool.SCRT_WeaponAbilityList[selectedPanID];
			CanvasList.instance.TXT_EquipName.text = SCRT_WeaponAbility.equipName;
			CanvasList.instance.TXT_EquipType.text = SCRT_WeaponAbility.equipType;
			ShowSelectButtonOrLockButton(SCRT_WeaponAbility.price, PlayerPrefs.GetInt("playerGem"), true, string.Empty);
		}
	}

	private void ShowHelmStat()
	{
		if (currentSelectedPanID != selectedPanID)
		{
			currentSelectedPanID = selectedPanID;
			CanvasList.instance.TXT_EquipType.text = AllEquipInfo.instance.helmList[selectedPanID].equipType;
			CanvasList.instance.TXT_EquipName.text = AllEquipInfo.instance.helmList[selectedPanID].equipName;
			ShowSelectButtonOrLockButton(AllEquipInfo.instance.helmList[selectedPanID].price, PlayerPrefs.GetInt("playerGem"), true, string.Empty);
		}
	}

	private void ShowCloakStat()
	{
		if (currentSelectedPanID != selectedPanID)
		{
			currentSelectedPanID = selectedPanID;
			CanvasList.instance.TXT_EquipType.text = "斗篷";
			CanvasList.instance.TXT_EquipName.text = VariousInfo.cloakInfo[selectedPanID].name;
			needHighscoreForUnlockingCloak = ((selectedPanID != 0) ? VariousInfo.hiddenBossAppearHighscoreList[selectedPanID - 1] : 0);
			ShowSelectButtonOrLockButton(selectedPanID, PlayerPrefs.GetInt("currentUnlockedCloakNumber"), false, (PlayerPrefs.GetInt("highestScore") >= needHighscoreForUnlockingCloak) ? "击败一个穿斗篷的人" : ("获得最高分 " + needHighscoreForUnlockingCloak));
		}
	}

	private void ShowSelectButtonOrLockButton(int price, int gem, bool allowAds = true, string str = "")
	{
		if (gem >= price)
		{
			CanvasList.instance.IMG_BTN_SelectTheEquip.sprite = Sprites.instance.etcSpriteList[0];
			GO_NeedGemAmount.SetActive(false);
			TimeReward.instance.ShowBTN_PlayVideoRewardAd(false);
			return;
		}
		CanvasList.instance.IMG_BTN_SelectTheEquip.sprite = Sprites.instance.etcSpriteList[1];
		GO_NeedGemAmount.SetActive(true);
		TXT_NeedGemAmount.text = ((!(str != string.Empty)) ? ("还需<sprite=0>X" + (price - PlayerPrefs.GetInt("playerGem"))) : str);
		if (allowAds)
		{
			TimeReward.instance.ShowBTN_PlayVideoRewardAd(true);
		}
	}

	public void Scrolling(bool scroll)
	{
		isScrolling = scroll;
		if (scroll)
		{
			scrollRect.inertia = true;
		}
	}

	public void SelectedEquipID()
	{
		if (!(CanvasList.instance.IMG_BTN_SelectTheEquip.sprite == Sprites.instance.etcSpriteList[1]))
		{
			if (CanvasList.instance.ShowingKindOfEquipList == "weaponList")
			{
				PlayerPrefs.SetInt("selectedWeaponID", selectedPanID);
			}
			else if (CanvasList.instance.ShowingKindOfEquipList == "helmList")
			{
				PlayerPrefs.SetInt("selectedHelmID", selectedPanID);
			}
			else if (CanvasList.instance.ShowingKindOfEquipList == "cloakList")
			{
				PlayerPrefs.SetInt("selectedCloakID", selectedPanID);
			}
			CanvasList.instance.Panel_Equip_Toggle();
			GM.instance.Init();
		}
	}
}
